<template>
  <div class="course">
    <Header></Header>
    <div class="main">
      <!-- 筛选条件 -->
      <div class="condition">
        <ul class="cate-list">
          <li class="title">课程分类:</li>
          <li
              @click="changeCategory(0)"
              :class="{ this: category === 0 }">
            全部
          </li>

          <li
              v-for="cat in category_list"
              :key="cat.id"
              @click="changeCategory(cat.id)"
              :class="{ this: category === cat.id }">
            {{ cat.name }}
          </li>
        </ul>

        <div class="ordering">
          <ul>
            <li class="title">筛&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;选:</li>
            <li @click="change_order_type(0)" class="default" :class="change_order_class(0)">默认</li>
            <li @click="change_order_type(1)" class="hot" :class="change_order_class(1)">人气</li>
            <li @click="change_order_type(2)" class="price" :class="change_order_class(2)">价格</li>
          </ul>
          <p class="condition-result">共{{total}}个课程</p>
        </div>
      </div>

      <!-- 课程列表 -->
      <div class="course-list">
        <div class="course-item" v-for="course in course_list" :key="course.id">
          <div class="course-image">
            <img :src="course.course_img" :alt="course.name">
          </div>
          <div class="course-info">
            <h3>
              <router-link :to="`/courses/${course.id}`">{{ course.name }}</router-link>
              <span><img src="../../public/static/image/avatar1.svg" alt="">{{ course.students }}已加入学习</span>
            </h3>
            <p class="teather-info">
              {{ course.teacher.name }} {{ course.teacher.signature }} {{ course.teacher.title }}
              <span class="lesson-info">共{{ course.lessons }}课时/{{ course.lessons == course.pub_lessons ? '更新完成' : `已更新${course.pub_lessons}课时` }}</span>
            </p>

            <ul class="lesson-list">
              <li v-for="(lesson, key) in course.lesson_list" :key="key">
                <p class="lesson-title">0{{ key + 1 }} | {{ lesson.name }}</p>
                <span v-if="lesson.free_trail" class="free">免费</span>
              </li>
            </ul>
            <div class="pay-box">
              <span class="discount-type">限时免费</span>
              <span class="discount-price">￥0.00元</span>
              <span class="original-price">原价：9.00元</span>
              <span class="buy-now">立即购买</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 分页 -->
    <el-pagination
        background
        layout="total, prev, pager, next, sizes"
        :page-size="filters.size"
        :page-sizes="[2,  5, 10, 20]"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        :total="total">
    </el-pagination>
    <Footer></Footer>
  </div>
</template>
<script>
import Header from "./common/Header"
import Footer from "./common/Footer"

export default {
  name: "Course",
  data() {
    return {
      total: 0,
      category: 0,
      ordering: "-id",
      course_list: [],
      category_list: [],
      filters: {
        type: 0, // 筛选类型，0表示默认，1表示人气,2表示价格
        orders: "desc",  // 排序类型，desc表示降序，asc表示升序
        page: 1, // 当前页
        size: 5 // 每页数量
      }
    }
  },
  created() {
    this.get_course_category();
    this.get_course();
  },
  methods: {
    // 页码切换时调用
    handleCurrentChange(page) {
      this.filters.page = page;
      this.get_course();
    },
    // 每页条数切换时调用
    handleSizeChange(size) {
      this.filters.size = size;
      this.filters.page = 1;
      this.get_course();
    },
    // 获取课程分类
    get_course_category() {
      this.$axios.get(`${this.$settings.HOST}/course/category/`).then(response => {
        this.category_list = response.data;
      })
    },
    // 获取课程信息
    get_course() {
      let params = {
        course_category: this.category === 0 ? undefined : this.category,
        ordering: this.get_ordering(),
        page: this.filters.page,
        size: this.filters.size
      };
      this.$axios.get(`${this.$settings.HOST}/course/`, {params}).then(response => {
        this.course_list = response.data.results;
        this.total = response.data.count;
      }).catch(error => {
        console.log(error);
      })
    },
    // 排序
    get_ordering() {
      let ordering = "";
      if (this.filters.type === 1) { // 人气排序
        ordering = this.filters.orders === "desc" ? "-students" : "students";
      } else if (this.filters.type === 2) { // 价格排序
        ordering = this.filters.orders === "desc" ? "-price" : "price";
      } else { // 默认排序
        ordering = this.filters.orders === "desc" ? "-id" : "id"
      }
      return ordering;
    },
    // 切换分类
    changeCategory(id) {
      this.category = id;
      this.get_course();
    },
    // 切换排序类型
    change_order_type(type) {
      if (this.filters.type === type && this.filters.orders === "desc") {
        this.filters.orders = "asc";
      } else if (this.filters.type === type && this.filters.orders === "asc") {
        this.filters.orders = "desc";
      } else {
        this.filters.orders = "desc";
      }
      this.filters.type = type;
      this.get_course();
    },
    // 排序样式
    change_order_class(type) {
      if (this.filters.type === type && this.filters.orders === "asc") {
        return "this asc";
      } else if (this.filters.type === type && this.filters.orders === "desc") {
        return "this desc";
      } else {
        return "";
      }
    }
  },
  components: {
    Header,
    Footer,
  }
}
</script>

<style scoped>
.course {
  background: #f6f6f6;
  padding: 20px 0;
}

.course .main {
  width: 1100px;
  margin: 0 auto;
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* 筛选条件 */
.course .condition {
  margin-bottom: 30px;
  padding: 20px;
  background: #fafafa;
  border-radius: 8px;
  border: 1px solid #eee;
}

.course .cate-list {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  padding-bottom: 15px;
  margin-bottom: 15px;
  border-bottom: 1px solid #eee;
}

.course .cate-list li {
  font-size: 14px;
  padding: 8px 16px;
  border-radius: 20px;
  cursor: pointer;
  color: #666;
  border: 1px solid #ddd;
  transition: all 0.3s ease;
}

.course .cate-list li:hover {
  border-color: #ffc210;
  color: #ffc210;
}

.course .cate-list li.this {
  background: #ffc210;
  color: #fff;
  border-color: #ffc210;
}

.course .ordering {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 15px;
}

.course .ordering ul {
  display: flex;
  align-items: center;
  gap: 15px;
}

.course .ordering ul li {
  font-size: 14px;
  padding: 8px 16px;
  border-radius: 20px;
  cursor: pointer;
  color: #666;
  border: 1px solid #ddd;
  transition: all 0.3s ease;
}

.course .ordering ul li:hover {
  border-color: #ffc210;
  color: #ffc210;
}

.course .ordering ul li.this {
  background: #ffc210;
  color: #fff;
  border-color: #ffc210;
}

.course .ordering .condition-result {
  font-size: 14px;
  color: #999;
}

/* 课程列表 */
.course .course-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.course .course-item {
  display: flex;
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  border: 1px solid #eee;
  transition: all 0.3s ease;
}

.course .course-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
}

.course .course-item .course-image {
  width: 250px;
  height: 150px;
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}

.course .course-item .course-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.course .course-item .course-info {
  flex: 1;
  margin-left: 20px;
}

.course-item .course-info h3 {
  font-size: 20px;
  color: #333;
  margin-bottom: 10px;
}

.course-item .course-info h3 a {
  color: inherit;
  text-decoration: none;
  transition: color 0.3s ease;
}

.course-item .course-info h3 a:hover {
  color: #ffc210;
}

.course-item .course-info h3 span {
  font-size: 14px;
  color: #999;
  float: right;
  margin-top: 5px;
}

.course-item .course-info h3 span img {
  width: 16px;
  height: 16px;
  vertical-align: middle;
  margin-right: 5px;
}

.course-item .course-info .teather-info {
  font-size: 14px;
  color: #999;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.course-item .course-info .teather-info .lesson-info {
  font-size: 14px;
  color: #666;
  background: #f5f5f5;
  padding: 4px 8px;
  border-radius: 4px;
  display: inline-block;
}

.course-item .lesson-list {
  margin-bottom: 15px;
}

.course-item .lesson-list li {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
  padding-left: 24px;
  background: url("../../public/static/image/play-icon-gray.svg") no-repeat left center;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.course-item .lesson-list li:hover {
  color: #ffc210;
  background-image: url("../../public/static/image/play-icon-yellow.svg");
}

.course-item .lesson-list li .lesson-title {
  flex: 1;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

.course-item .lesson-list li .free {
  font-size: 12px;
  color: #fd7b4d;
  border: 1px solid #fd7b4d;
  border-radius: 4px;
  padding: 2px 6px;
  margin-left: 10px;
  transition: all 0.3s ease;
}

.course-item .lesson-list li:hover .free {
  color: #ffc210;
  border-color: #ffc210;
}

.course-item .pay-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 15px;
}

.course-item .pay-box .discount-type {
  font-size: 14px;
  color: #fff;
  background: #fa6240;
  padding: 4px 8px;
  border-radius: 4px;
}

.course-item .pay-box .discount-price {
  font-size: 24px;
  color: #fa6240;
  font-weight: bold;
}

.course-item .pay-box .original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
}

.course-item .pay-box .buy-now {
  font-size: 16px;
  color: #fff;
  background: #ffc210;
  padding: 8px 20px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.course-item .pay-box .buy-now:hover {
  background: #ffa500;
}

/* 分页 */
.el-pagination {
  margin-top: 30px;
  text-align: center;
}
</style>